package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.approve;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.finance.BalanceEnquiryBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.COrderDetailMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CWeightSheetsMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.util.SzResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Component("ApproveExecutionBu")
public class ApproveExecutionBu {
    @Autowired
    private ICCompanysService iCCompanysService;
    @Autowired
    private ICOrderDetailService iCOrderDetailService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICFinaceEnterService iCFinaceEnterService;
    @Autowired
    private ICWeightSheetsService iCWeightSheetsService;
    @Autowired
    private ICCompanysService companysService;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ICPdtsAuthService iCPdtsAuthService;
    @Autowired
    private ITPriceRecordService itPriceRecordService;
    @Autowired
    private ICProductsInfoService icProductsInfoService;
    @Autowired
    private ICCompanysService icCompanysService;
    @Autowired
    private ICPdtPlanExService iCPdtPlanExService;
    @Autowired
    private COrderDetailMapper cOrderDetailMapper;
    @Autowired
    private CWeightSheetsMapper cWeightSheetsMapper;
    @Autowired
    private BalanceEnquiryBu balanceEnquiryBu;
    @Autowired
    private TCompetePdtsAuthService tCompetePdtsAuthService;

    @Transactional
    public void recharge(Object[] args) throws Exception {
        String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String plantId = jsonObject.getString("plantId");
        String financeEnterCoId = jsonObject.getString("financeEnterCoId");
        String financeEnterNum = jsonObject.getString("financeEnterNum");
        String targetCoId = jsonObject.getString("targetCoId");
        String financeEnterId = jsonObject.getString("financeEnterId");
//        SysUser approver = JSONObject.parseObject(args[1].toString(), SysUser.class);

        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();

        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", financeEnterCoId);
        queryWrapper.eq("CPY_DELETED", 0);
        queryWrapper.eq("CPY_PASS", 0);
        CCompanys cCompanys = iCCompanysService.getOne(queryWrapper);
        COrderDetail cOrderDetail;
        QueryWrapper<COrderDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ORDETAIL_CO", cCompanys.getCpyCo());
        queryWrapper1.eq("ORDETAIL_CHICO", cCompanys.getCpyChico());
        queryWrapper1.eq("ORDETAIL_SUB_ID", targetCoId);
//        queryWrapper1.eq("ORDETAIL_PDT", pdtId);
        cOrderDetail = iCOrderDetailService.getOne(queryWrapper1);
        if (cOrderDetail == null) {//没有这个账户
            //新增账户
            COrderDetail cOrderDetail1 = new COrderDetail();
            cOrderDetail1.setId(uuid);
            QueryWrapper<COrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
            orderDetailQueryWrapper.orderByDesc("ORDETAIL_ID");
            orderDetailQueryWrapper.last("limit 1");
            orderDetailQueryWrapper.eq("DATA_FROM", plantId);
            COrderDetail cOrderDetail2 = iCOrderDetailService.getOne(orderDetailQueryWrapper);
            if (cOrderDetail2 != null) {
                BigDecimal ordetailId = cOrderDetail2.getOrdetailId();
                String ordetailId1 = String.valueOf((ordetailId.intValue() + 1));
                cOrderDetail1.setOrdetailId(new BigDecimal(ordetailId1));
            } else {
                cOrderDetail1.setOrdetailId(new BigDecimal(1));
            }
                cOrderDetail1.setOrdetailItemNum(BigDecimal.valueOf(Long.parseLong(financeEnterNum)));
                cOrderDetail1.setOrdetailRemainNum(BigDecimal.valueOf(Long.parseLong(financeEnterNum)));
                cOrderDetail1.setOrdetailLastinNum(BigDecimal.valueOf(Long.parseLong(financeEnterNum)));
                if (Integer.parseInt(financeEnterNum) < 0) {
                    cOrderDetail1.setOrdetailLastoutNum(BigDecimal.valueOf(Long.parseLong(financeEnterNum)));
                } else {
                    cOrderDetail1.setOrdetailLastoutNum(BigDecimal.valueOf(0));
                }

            cOrderDetail1.setOrdetailCo(cCompanys.getCpyCo());
            cOrderDetail1.setOrdetailChico(cCompanys.getCpyChico());
            cOrderDetail1.setOrdetailSubId(targetCoId);
            cOrderDetail1.setOrdetailModifyTime(LocalDateTime.now());
            cOrderDetail1.setDataFrom(plantId);
            cOrderDetail1.setOrdetailDeleted(0);
            iCOrderDetailService.save(cOrderDetail1);
        } else {//有这个账户
            String ordetailId = cOrderDetail.getId();
            cOrderDetailMapper.insertOrderDetail(financeEnterNum, plantId, ordetailId);
        }
        UpdateWrapper<CFinaceEnter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", financeEnterId);
        updateWrapper.set("FINANCE_ENTERED", 1);
//        执行
        iCFinaceEnterService.update(updateWrapper);
    }


    @Transactional
    public void credit(Object[] args) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String credit = jsonObject.getString("credit");
        String co = jsonObject.getString("co");
        String chiCo = jsonObject.getString("chiCo");
        String subCoId = jsonObject.getString("subCoId");
        String plantId = jsonObject.getString("plantId");
        // String planId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        QueryWrapper<COrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ORDETAIL_CO", co);
        queryWrapper.eq("ORDETAIL_CHICO", chiCo);
        queryWrapper.eq("ORDETAIL_SUB_ID", BigDecimal.valueOf(Long.parseLong(subCoId)));
        COrderDetail cOrderDetail = iCOrderDetailService.getOne(queryWrapper);
        if (cOrderDetail == null) {
            throw new Exception("没有这个账户");
        } else {
            cOrderDetail.setOrdetailPreLic(BigDecimal.valueOf(Long.parseLong(credit)));
            cOrderDetail.setDownedID1(0);
            cOrderDetail.setDataFrom(plantId);
            cOrderDetail.setOrdetailDeleted(0);
            iCOrderDetailService.updateById(cOrderDetail);
        }
    }

    /**
     * 单据调价
     *
     * @param args
     * @throws Exception
     */
    @Transactional
    public void updateWeightSheetPrice(Object[] args) throws Exception {
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String price = jsonObject.getString("price");
        String plantId = jsonObject.getString("plantId");
        String trueName = jsonObject.getString("trueName");
        String userId = jsonObject.getString("userId");
        List<String> weishtSheetnos = jsonObject.getJSONArray("weishtSheetnos").toJavaList(String.class);
        for (String weishtSheetno : weishtSheetnos) {
            QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("WEISHT_SHEETNO", weishtSheetno);
            queryWrapper.eq("DATA_FROM", plantId);
            CWeightSheets cWeightSheets = iCWeightSheetsService.getOne(queryWrapper);
            if (cWeightSheets == null) {
                throw new Exception("没有这个磅单");
            } else {

                //  cWeightSheetsMapper.updateWeightSheet(price);


//            老钱
                String oldMoney = cWeightSheets.getWeishtMoney().toString();
                //        logMsg里记录一下磅单号，由什么价格调整为什么价格
                logMsg.append("磅单号【").append(cWeightSheets.getWeishtSheetno()).append("】由").append(cWeightSheets.getWeishtPrice()).append("调整为").append(price).append(";");
                cWeightSheets.setWeishtPrice(BigDecimal.valueOf(Double.parseDouble(price)));
//            将这一单的钱改变
                cWeightSheets.setWeishtMoney(BigDecimal.valueOf(Double.parseDouble(price)).multiply(cWeightSheets.getWeishtNetWght()));
//            新钱
                String newMoney = cWeightSheets.getWeishtMoney().toString();
                cWeightSheets.setDownedID1(0);
                iCWeightSheetsService.updateById(cWeightSheets);


                QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("CPY_CO", cWeightSheets.getWeishtCoSupply());
                queryWrapper2.eq("CPY_CHICO", cWeightSheets.getWeishtCoSupplyChi());
                queryWrapper2.eq("DATA_FROM", cWeightSheets.getDataFrom());
                queryWrapper2.eq("CPY_DELETED", 0);
                queryWrapper2.eq("DATA_FROM", plantId);
                CCompanys cCompanys = companysService.getOne(queryWrapper2);
                if (cCompanys == null) {
                    throw new Exception("供货单位不存在");
                }
                //         将钱退回账户
                QueryWrapper<COrderDetail> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ORDETAIL_CO", cWeightSheets.getWeishtCo());
                queryWrapper1.eq("ORDETAIL_CHICO", cWeightSheets.getWeishtChiCo());
                queryWrapper1.eq("ORDETAIL_SUB_ID", cCompanys.getCpyId());
                queryWrapper1.eq("DATA_FROM", cWeightSheets.getDataFrom());
                queryWrapper1.eq("ORDETAIL_DELETED", 0);
                COrderDetail cOrderDetail = iCOrderDetailService.getOne(queryWrapper1);
                UpdateWrapper<COrderDetail> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("ID", cOrderDetail.getId());
                updateWrapper.set("DownedID1", 0);
                iCOrderDetailService.update(updateWrapper);
                String cOrderDetailId = cOrderDetail.getId();
//      COrderDetail表的  ORDETAIL_REMAIN_NUM是余额，cWeightSheets表的WEISHT_MONEY是磅单金额都是BigDecimal类型
//            cOrderDetail.setOrdetailRemainNum(cOrderDetail.getOrdetailRemainNum().add(cWeightSheets1.getWeishtMoney()));加一个负数
               /* cOrderDetail.setOrdetailRemainNum(cOrderDetail.getOrdetailRemainNum().add(BigDecimal.valueOf(Double.parseDouble(oldMoney)).subtract(BigDecimal.valueOf(Double.parseDouble(newMoney)))));
                iCOrderDetailService.updateById(cOrderDetail);*/
                cOrderDetailMapper.updateOrderDetail(BigDecimal.valueOf(Long.parseLong(oldMoney)), BigDecimal.valueOf(Long.parseLong(newMoney)), cOrderDetailId);
                sysLogBu.logRecord("单据调价", trueName, "调价了" + weishtSheetnos.size() + "条磅单，" + logMsg, plantId);
            }
        }
    }


    /**
     * 一键调价
     */
    @Transactional
    public void updateWeightSheetPriceAll(Object[] args) throws Exception {
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String price = jsonObject.getString("price");
        String plantId = jsonObject.getString("plantId");
        String trueName = jsonObject.getString("trueName");
        String userId = jsonObject.getString("userId");
        String fullTimeStart = jsonObject.getString("fullTimeStart");
        String fullTimeEnd = jsonObject.getString("fullTimeEnd");
        List<String> no = jsonObject.getJSONArray("no").toJavaList(String.class);
        for (String weishtSheetno : no) {
            QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("WEISHT_SHEETNO", weishtSheetno);
            queryWrapper.eq("DATA_FROM", plantId);
            CWeightSheets cWeightSheets = iCWeightSheetsService.getOne(queryWrapper);
            if (cWeightSheets == null) {
                throw new Exception("没有这个磅单");
            } else {
//            老钱
                String oldMoney = cWeightSheets.getWeishtMoney().toString();
                //        logMsg里记录一下磅单号，由什么价格调整为什么价格
                logMsg.append("磅单号【").append(cWeightSheets.getWeishtSheetno()).append("】由").append(cWeightSheets.getWeishtPrice()).append("调整为").append(price).append(";");
                cWeightSheets.setWeishtPrice(BigDecimal.valueOf(Double.parseDouble(price)));
//            将这一单的钱改变
                cWeightSheets.setWeishtMoney(BigDecimal.valueOf(Double.parseDouble(price)).multiply(cWeightSheets.getWeishtNetWght()));
//            新钱
                String newMoney = cWeightSheets.getWeishtMoney().toString();
                cWeightSheets.setDownedID1(0);
                iCWeightSheetsService.updateById(cWeightSheets);
                QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("CPY_CO", cWeightSheets.getWeishtCoSupply());
                queryWrapper2.eq("CPY_CHICO", cWeightSheets.getWeishtCoSupplyChi());
                queryWrapper2.eq("DATA_FROM", cWeightSheets.getDataFrom());
                queryWrapper2.eq("CPY_DELETED", 0);
                CCompanys cCompanys = companysService.getOne(queryWrapper2);
                if (cCompanys == null) {
                    throw new Exception("供货单位不存在");
                }
                //         将钱退回账户
                QueryWrapper<COrderDetail> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ORDETAIL_CO", cWeightSheets.getWeishtCo());
                queryWrapper1.eq("ORDETAIL_CHICO", cWeightSheets.getWeishtChiCo());
                queryWrapper1.eq("ORDETAIL_SUB_ID", cCompanys.getCpyId());
                queryWrapper1.eq("DATA_FROM", cWeightSheets.getDataFrom());
                queryWrapper1.eq("ORDETAIL_DELETED", 0);
                COrderDetail cOrderDetail = iCOrderDetailService.getOne(queryWrapper1);
//      COrderDetail表的  ORDETAIL_REMAIN_NUM是余额，cWeightSheets表的WEISHT_MONEY是磅单金额都是BigDecimal类型
//            cOrderDetail.setOrdetailRemainNum(cOrderDetail.getOrdetailRemainNum().add(cWeightSheets1.getWeishtMoney()));加一个负数
        /*        cOrderDetail.setOrdetailRemainNum(cOrderDetail.getOrdetailRemainNum().add(BigDecimal.valueOf(Double.parseDouble(oldMoney)).subtract(BigDecimal.valueOf(Double.parseDouble(newMoney)))));
                iCOrderDetailService.updateById(cOrderDetail);*/
                String cOrderDetailId = cOrderDetail.getId();
                UpdateWrapper<COrderDetail> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("ID", cOrderDetail.getId());
                updateWrapper.set("DownedID1", 0);
                iCOrderDetailService.update(updateWrapper);
                cOrderDetailMapper.updateOrderDetail(BigDecimal.valueOf(Long.parseLong(oldMoney)), BigDecimal.valueOf(Long.parseLong(newMoney)), cOrderDetailId);
                sysLogBu.logRecord("单据调价", trueName, "调价了" + no.size() + "条磅单，" + logMsg, plantId);
            }
        }
    }

    /**
     * 产品调价
     */
    @Transactional
    public void updatePdtPrice(Object[] args) throws Exception {
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String userId = jsonObject.getString("userId");
        String trueName = jsonObject.getString("trueName");
        String price = jsonObject.getString("price");
        String plantId = jsonObject.getString("plantId");
        List<String> ids = jsonObject.getJSONArray("ids").toJavaList(String.class);
        for (String s : ids) {
            QueryWrapper<CPdtsAuth> authQueryWrapper = new QueryWrapper<>();
            authQueryWrapper.eq("ID", s);
            CPdtsAuth cPdtsAuth = iCPdtsAuthService.getOne(authQueryWrapper);
            cPdtsAuth.setPdtsauthReason(LocalDateTime.now().toString().replace("T"," ") + "由" + trueName + "调价：" + cPdtsAuth.getPdtsauthPrice() + "->" + price);
//                "调整" + cPdtsAuth.getPdtsauthCo() + "的" + cPdtsAuth.getPdtsauthPdt() + "价格由" + cPdtsAuth.getPdtsauthPrice() + "调整为" + price
            logMsg.append("调整").append(cPdtsAuth.getPdtsauthCo()).append("的").append(cPdtsAuth.getPdtsauthPdt()).append("价格由").append(cPdtsAuth.getPdtsauthPrice()).append("调整为").append(price);
            cPdtsAuth.setPdtsauthPrice(BigDecimal.valueOf(Double.parseDouble(price)));
            iCPdtsAuthService.updateById(cPdtsAuth);
            String recordNo = LocalDateTime.now().getYear() + String.valueOf(LocalDateTime.now().getMonthValue() < 10 ? "0" + LocalDateTime.now().getMonthValue() : LocalDateTime.now().getMonthValue()) + (LocalDateTime.now().getDayOfMonth() < 10 ? "0" + LocalDateTime.now().getDayOfMonth() : LocalDateTime.now().getDayOfMonth());
//                拿到TPriceRecord表里RECORD_ID的最大值
            QueryWrapper<TPriceRecord> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.like("RECORD_NO", recordNo);
            queryWrapper2.orderByDesc("RECORD_NO");
            queryWrapper2.last("limit 1");
            TPriceRecord tPriceRecord1 = itPriceRecordService.getOne(queryWrapper2);
            TPriceRecord tPriceRecord = new TPriceRecord();
            tPriceRecord.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
            QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PDTINF_PDT", cPdtsAuth.getPdtsauthPdt());
            queryWrapper.eq("PDTINF_DELETED", 0);
            CProductsInfo cProductsInfo = icProductsInfoService.getOne(queryWrapper);
            tPriceRecord.setPdtId(cProductsInfo.getId());
            tPriceRecord.setCreateTime(LocalDateTime.now());
            tPriceRecord.setCreatorId(userId);
            QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CPY_CO", cPdtsAuth.getPdtsauthCo());
            queryWrapper1.eq("CPY_CHICO", cPdtsAuth.getPdtsauthChico());
            queryWrapper1.eq("CPY_DELETED", 0);
            CCompanys cCompanys = icCompanysService.getOne(queryWrapper1);
            tPriceRecord.setCpyId(cCompanys.getId());
            tPriceRecord.setCreatorName(trueName);
            if (tPriceRecord1 == null) {
                tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(recordNo + "0001")));
            } else {
                tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(tPriceRecord1.getRecordNo().toString()) + 1));
            }
            tPriceRecord.setDataFrom(plantId);
            itPriceRecordService.save(tPriceRecord);
            sysLogBu.logRecord("产品调价", trueName, String.valueOf(logMsg), plantId);
        }
    }


    //品种授权
    @Transactional
    public void pdtAuth(Object[] args) throws Exception {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        CPdtsAuth cPdtsAuth = jsonObject.getObject("cPdtsAuth", CPdtsAuth.class);
        String pdtinfSubjectId = jsonObject.getString("pdtinfSubjectId");
        String pdtsauthPdt = cPdtsAuth.getPdtsauthPdt();
        String pdtsauthCo = cPdtsAuth.getPdtsauthCo();
        String pdtsauthChico = cPdtsAuth.getPdtsauthChico();
        String plantId = cPdtsAuth.getDataFrom();
        iCPdtsAuthService.save(cPdtsAuth);
        COrderDetail cOrderDetail = new COrderDetail();
        cOrderDetail.setId(uuid);
        QueryWrapper<COrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.orderByDesc("ORDETAIL_ID");
        orderDetailQueryWrapper.last("limit 1");
        orderDetailQueryWrapper.eq("DATA_FROM", plantId);
        COrderDetail cOrderDetail2 = iCOrderDetailService.getOne(orderDetailQueryWrapper);
        if (cOrderDetail2 != null) {
            BigDecimal ordetailId = cOrderDetail2.getOrdetailId();
            String ordetailId1 = String.valueOf((ordetailId.intValue() + 1));
            cOrderDetail.setOrdetailId(new BigDecimal(ordetailId1));
        } else {
            cOrderDetail.setOrdetailId(new BigDecimal(1));
        }
        cOrderDetail.setOrdetailSubId(pdtinfSubjectId);
        cOrderDetail.setOrdetailPdt(pdtsauthPdt);
        cOrderDetail.setOrdetailCo(pdtsauthCo);
        cOrderDetail.setOrdetailChico(pdtsauthChico);
        cOrderDetail.setOrdetailItemNum(BigDecimal.valueOf(0));
        cOrderDetail.setOrdetailRemainNum(BigDecimal.valueOf(0));
        cOrderDetail.setOrdetailPreLic(BigDecimal.valueOf(0));
        cOrderDetail.setOrdetailLastoutNum(BigDecimal.valueOf(0));
        cOrderDetail.setOrdetailLastinNum(BigDecimal.valueOf(0));
        cOrderDetail.setOrdetailDeleted(0);
        cOrderDetail.setDataFrom(plantId);
        cOrderDetail.setDownedID1(0);
        iCOrderDetailService.save(cOrderDetail);
    }






    //新增发货计划
    @Transactional
    public void addDeliveryPlan(Object[] args) throws Exception {
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        CPdtPlanEx cPdtPlanEx = jsonObject.getObject("cPdtPlanEx", CPdtPlanEx.class);
        String trueName = jsonObject.getString("trueName");
        String cpy = jsonObject.getString("cpy");
        String pdtinfPdt = jsonObject.getString("pdt");
        String planType = jsonObject.getString("planType");
        String planDays = jsonObject.getString("planDays");
        String planNum = jsonObject.getString("planNum");
        String planStartTime = jsonObject.getString("planStartTime");
        planStartTime = planStartTime.replace("T", "");
        iCPdtPlanExService.save(cPdtPlanEx);
        sysLogBu.logRecord("新增发货计划", trueName, "新增一条" + cpy + "公司的" + pdtinfPdt + "品种的发货计划,计划类型是:" + planType + " 计划天数是:" + planDays + " 计划量是:" + planNum + "计划开始时间是:" + planStartTime, cPdtPlanEx.getDataFrom());
    }


    /**
     * 删除充值记录
     */
    @Transactional
    public void deleteRechargeRecord(Object[] args) throws Exception {
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String id = jsonObject.getString("id");
        String plantId = jsonObject.getString("plantId");
        String trueName = jsonObject.getString("trueName");
        CFinaceEnter cFinaceEnter = iCFinaceEnterService.getById(id);


        Integer financeEntered = cFinaceEnter.getFinanceEntered();
        if (financeEntered == 1) {//已入账
            BigDecimal financeEnterNum = cFinaceEnter.getFinanceEnterNum();
            //余额减去充值金额
            QueryWrapper<COrderDetail> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("ORDETAIL_CO", cFinaceEnter.getFinanceEnterCo());
            queryWrapper2.eq("ORDETAIL_CHICO", cFinaceEnter.getFinanceEnterChiCo());
            queryWrapper2.eq("ORDETAIL_SUB_ID", cFinaceEnter.getFinanceSubjectId());
            queryWrapper2.eq("ORDETAIL_DELETED", 0);
            COrderDetail one = iCOrderDetailService.getOne(queryWrapper2);
         /*   if (one == null) {
                return SzResult.error("该公司的余额账户不存在");
            }*/
            BigDecimal ordetailRemainNum = one.getOrdetailRemainNum();
            BigDecimal subtract = ordetailRemainNum.subtract(financeEnterNum);
//            总计充值金额减去充值金额
            BigDecimal financeEnterTotal = one.getOrdetailItemNum();
            BigDecimal subtract1 = financeEnterTotal.subtract(financeEnterNum);
            one.setOrdetailRemainNum(subtract);
            one.setOrdetailItemNum(subtract1);
            one.setDownedID1(0);
            iCOrderDetailService.updateById(one);
            cFinaceEnter.setFinanceEnterDeleted(1);
        } else if (financeEntered == 0) {
            cFinaceEnter.setFinanceEnterDeleted(1);
            cFinaceEnter.setDownedID1(0);
        }
        iCFinaceEnterService.updateById(cFinaceEnter);
        String financeSubjectId = cFinaceEnter.getFinanceSubjectId();
        QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("ID", financeSubjectId);
        CCompanys cCompanys = iCCompanysService.getOne(queryWrapper2);
        logMsg.append("删除了款项进账记录【").append("/n").append("充值编号为:").append(cFinaceEnter.getFinanceEnterId()).append("/n").append("充值公司为:").append(cFinaceEnter.getFinanceEnterCo()).append(".").append(cFinaceEnter.getFinanceEnterChiCo()).append("/n").append("收款单位为:").append(cCompanys.getCpyCo()).append(".").append(cCompanys.getCpyChico()).append("/n").append("充值金额为:").append(cFinaceEnter.getFinanceEnterNum()).append("的记录】");

        sysLogBu.logRecord("款项进账", trueName, logMsg.toString(), plantId);
    }


    /**
     * 充值记录设置rechargeRecordSetting
     */
    @Transactional
    public void rechargeRecordSetting(Object[] args) throws Exception {
        StringBuilder logMsg = new StringBuilder();
        JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
        String id = jsonObject.getString("id");
        String plantId = jsonObject.getString("plantId");
        String trueName = jsonObject.getString("trueName");
        String sfnFianceEnterStatus = jsonObject.getString("sfnFianceEnterStatus");
        CFinaceEnter cFinaceEnter = iCFinaceEnterService.getById(id);

        String financeEnterCo = cFinaceEnter.getFinanceEnterCo();
        String financeEnterChiCo = cFinaceEnter.getFinanceEnterChiCo();
        QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("CPY_CO", financeEnterCo);
        queryWrapper2.eq("CPY_CHICO", financeEnterChiCo);
        queryWrapper2.eq("CPY_DELETED", 0);
        CCompanys cCompanys = iCCompanysService.getOne(queryWrapper2);
        String cpyId = String.valueOf(cCompanys.getCpyId());

        Integer financeEntered = cFinaceEnter.getFinanceEntered();
        if (financeEntered == 0 && Integer.parseInt(sfnFianceEnterStatus) == 1) {//通过

            //打款
            String financeEnterNum = String.valueOf(cFinaceEnter.getFinanceEnterNum());
            String financeEnterCoId = String.valueOf(cpyId);
            String targetCoId = String.valueOf(cFinaceEnter.getFinanceSubjectId());
            balanceEnquiryBu.addOrderDetail(financeEnterNum, financeEnterCoId, targetCoId, plantId);
            cFinaceEnter.setFinanceEntered(1);
            logMsg.append("修改了款项进账记录【").append("充值编号:").append(cFinaceEnter.getFinanceEnterId()).append("充值公司:").append(cFinaceEnter.getFinanceEnterCo()).append(".").append(cFinaceEnter.getFinanceEnterChiCo()).append("收款单位:").append(cCompanys.getCpyCo()).append(".").append(cCompanys.getCpyChico()).append("的记录的是否充值成功由不通过改为通过").append("】");


        } else if (financeEntered == 1 && Integer.parseInt(sfnFianceEnterStatus) == 0) {//bu通过
            //扣钱
            String financeEnterNum = String.valueOf(cFinaceEnter.getFinanceEnterNum());
            QueryWrapper<COrderDetail> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("ORDETAIL_CO", financeEnterCo);
            queryWrapper4.eq("ORDETAIL_CHICO", financeEnterChiCo);
            queryWrapper4.eq("ORDETAIL_SUB_ID", cFinaceEnter.getFinanceSubjectId());
            queryWrapper4.eq("ORDETAIL_DELETED", 0);
            COrderDetail one = iCOrderDetailService.getOne(queryWrapper4);
            BigDecimal ordetailRemainNum = one.getOrdetailRemainNum();
            BigDecimal subtract = ordetailRemainNum.subtract(new BigDecimal(financeEnterNum));
            one.setOrdetailRemainNum(subtract);
            one.setDownedID1(0);
            iCOrderDetailService.updateById(one);
            cFinaceEnter.setFinanceEntered(0);
            logMsg.append("修改了款项进账记录【").append("充值编号:").append(cFinaceEnter.getFinanceEnterId()).append("充值公司:").append(cFinaceEnter.getFinanceEnterCo()).append(".").append(cFinaceEnter.getFinanceEnterChiCo()).append("收款单位:").append(cCompanys.getCpyCo()).append(".").append(cCompanys.getCpyChico()).append("的记录的是否充值成功由通过改为不通过").append("】");
        }
        iCFinaceEnterService.updateById(cFinaceEnter);
    }

//竞标品种授权
//品种授权
@Transactional
public void competePdtAuth(Object[] args) throws Exception {
    StringBuilder logMsg = new StringBuilder();
    JSONObject jsonObject = JSONObject.parseObject(args[0].toString());
    TCompetePdtsAuth cPdtsAuth = jsonObject.getObject("competePdtsAuth", TCompetePdtsAuth.class);
    String pdtinfSubjectId = jsonObject.getString("pdtinfSubjectId");
    String pdtsauthPdt = cPdtsAuth.getPdtsauthPdt();
    String pdtsauthCo = cPdtsAuth.getPdtsauthCo();
    String pdtsauthChico = cPdtsAuth.getPdtsauthChico();
    String plantId = cPdtsAuth.getDataFrom();
    tCompetePdtsAuthService.save(cPdtsAuth);
}

}